package cst.a112gxl.TeacherPlatform112gxl.fragment;


import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import java.util.List;

import cst.a112gxl.TeacherPlatform112gxl.R;
import cst.a112gxl.TeacherPlatform112gxl.adapter.CArticleAdapter;
import cst.a112gxl.TeacherPlatform112gxl.bean.ArticleBean;
import cst.a112gxl.TeacherPlatform112gxl.bean.CollectBean;
import cst.a112gxl.TeacherPlatform112gxl.listener.AmicoolListener;
import cst.a112gxl.TeacherPlatform112gxl.model.Resmodel;

/**
 * Created by Administrator on 2017/6/9/009.
 */

public  class CFragment<T> extends Fragment {
    private View view=null;
    private Context context;
    private String mod,mod2;
    private int userid;
    private RecyclerView recyclerView;
    private LinearLayoutManager layoutManager;//显示布局效果
    private CArticleAdapter adapter;//适配器
    private List<CollectBean<ArticleBean>> list=null;//数据源


    private SharedPreferences sp;
    private String sessionID="";

    private int page=1;// 代表页数，并初始化为1，代表第1页。
    private int lastVisibleItemPosition;//最后一条可见条目的位置

    public static CFragment newfragment(Bundle args) {
        CFragment f = new CFragment();
        f.setArguments(args);
        return f;
    }



    AmicoolListener<List<CollectBean<ArticleBean>>> listListener = new AmicoolListener<List<CollectBean<ArticleBean>>>() {
    @Override
    public void onResponse(List<CollectBean<ArticleBean>> l) {
        if(page==1) {
            list=l;
        }
        else {
            list.removeAll(l);
            list.addAll(l);
        }
        adapter.setList(list);//传给adapter
        adapter.notifyDataSetChanged();//通知更新
    }

    @Override
    public void onFail(String msg) {
        Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
    }
};



    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        context=getActivity();
        Bundle bundle = getArguments();
        mod = bundle.getString("mod");
        //动态加载Fragment1的布局文件
        view=inflater.inflate(R.layout.article_fragment,container,false);
        sp = context.getSharedPreferences("teach",Context.MODE_PRIVATE);

        readSP();

        System.out.println("----onCreateView page="+page);

        //实例化ArticleModel，调用方法获取网络数据
        Resmodel resmodel=new Resmodel("gson");
        if (mod.equals("focuslist"))
            resmodel.getFocusArticletList(mod,page,sessionID,userid,listListener);
        else
            resmodel.getCollectList(mod,page,sessionID,listListener);

        initRecyclerView();
        //返回动态生成的view
        return view;
    }
    private void readSP() {
        sessionID=sp.getString("sessionid",null);

    }
    private void initRecyclerView() {
        //获取RecyclerView，设置属性，获取数据源，绑定
        recyclerView=(RecyclerView)view.findViewById(R.id.recycleView);
        //创建默认的线性布局
        layoutManager=new LinearLayoutManager(context);
        //设置布局管理器
        recyclerView.setLayoutManager(layoutManager);
        //固定每个item高度，提高性能
        recyclerView.setHasFixedSize(true);
        //创建Adaper
        adapter =new CArticleAdapter(context,mod);
        adapter.setList(list);
        //绑定RecyclerView和adapter
        recyclerView.setAdapter(adapter);
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == RecyclerView.SCROLL_STATE_IDLE && lastVisibleItemPosition + 1 == list.size()) {
                    page += 1;
                    //再次实例化ArticleModel，调用方法获取网络数据，请求新一页数据
                    Resmodel resmodel=new Resmodel("gson");
                    if (mod2.equals("focuslist"))
                        resmodel.getFocusArticletList(mod,page,sessionID,userid,listListener);
                    else
                        resmodel.getCollectList(mod,page,sessionID,listListener);
                    System.out.println("----onScrollStateChanged  page="+page);
                }
            }
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                lastVisibleItemPosition=layoutManager.findLastVisibleItemPosition();//滚动结束后将赋值为可见条目中最后一条位置
                //lastVisibleItemPosition = list.size() - 1;
            }
        });

    }
}